class RLEIterator(object):

    def __init__(self, encoding):
        """
        :type encoding: List[int]
        """
        self.encoding = encoding
        self.index = 0
        self.length = len(self.encoding) // 2


    def next(self, n):
        """
        :type n: int
        :rtype: int
        """
        while self.index < self.length and n > 0:
            if self.encoding[2 * self.index] >= n:
                self.encoding[2 * self.index] -= n
                n = 0
            else:
                n -= self.encoding[2 * self.index]
                self.encoding[2 * self.index] = 0
                self.index += 1
        if n > 0:
            return -1
        else:
            return self.encoding[2 * self.index + 1]


